package com.cqc.paoclient;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.cqc.paoclient.base.BaseActivity;
import com.cqc.paoclient.base.BasePresenter;
import com.cqc.paoclient.ui.aritcle.Tab1Frag;
import com.cqc.paoclient.ui.code.Tab2Frag;
import com.cqc.paoclient.ui.mime.Tab3Frag;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MainActivity extends BaseActivity {

    @BindView(R.id.bottomBar)
    BottomNavigationBar bottomBar;

    private List<Fragment> fragList = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }


    @Override
    public BasePresenter getmPresenter() {
        return null;
    }

    @Override
    public void presenterSetView() {

    }

    @Override
    public void initView() {
        //这样写无效，还是都从资源文件中获取吧
        //        bottomBar.addItem(new BottomNavigationItem(R.drawable.article, "文章"))
        //                .addItem(new BottomNavigationItem(R.drawable.code, "代码"))
        //                .addItem(new BottomNavigationItem(R.drawable.mine, "我的"))
        //                .setFirstSelectedPosition(0)
        //                .invalidate();

        bottomBar.addItem(new BottomNavigationItem(R.drawable.article, R.string.article))
                .addItem(new BottomNavigationItem(R.drawable.code, R.string.code))
                .addItem(new BottomNavigationItem(R.drawable.mine, R.string.mine))
                .setFirstSelectedPosition(0)
                .initialise();

        bottomBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                switchFrag(position);
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });

        initFragList();
        //switchFrag(0);
        bottomBar.selectTab(0);
    }

    private void initFragList() {
        fragList.add(new Tab1Frag());
        fragList.add(new Tab2Frag());
        fragList.add(new Tab3Frag());
    }

    private int lastIndex;

    private void switchFrag(int position) {
        if (position >= fragList.size()) {
            return;
        }
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        Fragment targetFrag = fragList.get(position);
       /*
       Fragment lastFrag = fragList.get(lastIndex);
        lastIndex = position;
        ft.hide(lastFrag);
        if (!targetFrag.isAdded()) {
            ft.add(R.id.fl_content, targetFrag);
        }
        ft.show(targetFrag).commitAllowingStateLoss();
        */

        //为什么不用add()+show()+hide()的方式展示fragment，因为TabFrag2里面加载了大量的gif图片，add()方式导致3个Fragment都会存如内存中，导致
        //占用了大量的cpu
        ft.replace(R.id.fl_content, targetFrag).commitAllowingStateLoss();
    }

    @Override
    public void initData() {
    }

}
